package maa.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FieldValidator {
    private static final Logger log = LoggerFactory.getLogger(FieldValidator.class);

    public static boolean isValidEmail(String email) {
        String emailRegex = "(([a-zA-Z][\\w]*)@[\\w]*\\.{1}([a-z]+))";
        Pattern pattern = Pattern.compile(emailRegex);
        Matcher matcher = pattern.matcher(email);
        if (!matcher.matches()) {
            log.debug("User email is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidPassword(String password) {
        String passwordRegex = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%-_]).{6,20})";
        Pattern pattern = Pattern.compile(passwordRegex);
        Matcher matcher = pattern.matcher(password);
        if (!matcher.matches()) {
            log.debug("User password is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidName(String name) {
        String nameRegex = "[A-ZА-Я]{1}[a-zA-Zа-яА-Я]{0,30}";
        Pattern pattern = Pattern.compile(nameRegex);
        Matcher matcher = pattern.matcher(name);
        if (!matcher.matches()) {
            log.debug("User name is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidSurname(String surname) {
        String surnameRegex = "([A-ZА-Я]{1}[a-zA-Zа-яА-Я]{0,40})";
        Pattern pattern = Pattern.compile(surnameRegex);
        Matcher matcher = pattern.matcher(surname);
        if (!matcher.matches()) {
            log.debug("User surname is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidBirthdate(String date) {
        String dateRegex = "([12][90][0-9]{2}[-][01][0-9][-][0-3][0-9])";
        Pattern pattern = Pattern.compile(dateRegex);
        Matcher matcher = pattern.matcher(date);
        if (!matcher.matches()) {
            log.debug("User brithdate is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidGender(String gender) {
        if (gender.equals("MALE") || gender.equals("FEMALE")) {
            return true;
        } else {
            log.debug("User gender is not valid.");
            return false;
        }
    }

    public static boolean isValidCourseName(String courseName) {
        String nameRegex = "[A-ZА-Я]{1}[a-zA-Zа-яА-Я.,?!-':;&*() ]{0,50}";
        Pattern pattern = Pattern.compile(nameRegex);
        Matcher matcher = pattern.matcher(courseName);
        if (!matcher.matches()) {
            log.debug("Course name is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidCourseDescription(String description) {
        String nameRegex = "[A-ZА-Я]{1}[\\w.,?!-':;&*() а-яА-Я\\s]{0,500}";
        Pattern pattern = Pattern.compile(nameRegex);
        Matcher matcher = pattern.matcher(description);
        if (!matcher.matches()) {
            log.debug("Course description is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidPrice(String price) {
        String nameRegex = "[0-9]{1,10}[.][0-9]{0,10}";
        Pattern pattern = Pattern.compile(nameRegex);
        Matcher matcher = pattern.matcher(price);
        if (!matcher.matches()) {
            log.debug("Course description is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidRang(String rang) {
        String nameRegex = "[A-ZА-Я0-9]{1}[.,?!-':;&*() 0-9a-zA-Zа-яА-Я]{0,50}";
        Pattern pattern = Pattern.compile(nameRegex);
        Matcher matcher = pattern.matcher(rang);
        if (!matcher.matches()) {
            log.debug("Course description is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidInfo(String info) {
        String nameRegex = "[A-ZА-Я]{1}[\\w.,?!-':;&*() а-яА-Я\\s]{0,300}";
        Pattern pattern = Pattern.compile(nameRegex);
        Matcher matcher = pattern.matcher(info);
        if (!matcher.matches()) {
            log.debug("Course description is not valid.");
            return false;
        }
        return true;
    }

    public static boolean isValidWorkExperience(String exp) {
        String nameRegex = "[0-9]{0,2}[.][0-9]{0,2}";
        Pattern pattern = Pattern.compile(nameRegex);
        Matcher matcher = pattern.matcher(exp);
        if (!matcher.matches()) {
            log.debug("Course description is not valid.");
            return false;
        }
        return true;
    }
}

